package com.app.micaihu.view.main.msgcenter.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.message.NewEditorRmdBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.utils.util.image.LoadImageUtils;

import java.util.List;

/**
 * Created by Guo on 2016/11/8.
 * <p>
 * 2.0.2版本小编特荐
 */
public class NewEditorRecommendAdapter extends AppBaseAdapter<NewEditorRmdBean> implements View.OnClickListener {


    public NewEditorRecommendAdapter(List<NewEditorRmdBean> list, Context context) {
        super(list, context);
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewH viewH;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_neweditor, null);
            viewH = new ViewH(convertView);
            convertView.setTag(viewH);
        } else {
            viewH = (ViewH) convertView.getTag();
        }
        NewEditorRmdBean newEditorRmdBean = list.get(position);
        if (newEditorRmdBean != null) {
            viewH.tvDesc.setText(newEditorRmdBean.getArticleDesc());
            viewH.tvPushTime.setText(newEditorRmdBean.getTime());
            viewH.tvTitile.setText(newEditorRmdBean.getArticleTitle());
            LoadImageUtils.getInstance().loadImage(viewH.civThumb, newEditorRmdBean.getArticleThumb());
//            LoadImageUtils.getInstance().loadImage(viewH.civHead,newEditorRmdBean.getEditorHeadPic());
            LoadImageUtils.getInstance().loadImage(viewH.civHead, R.drawable.news_list_recom_huge, true);


            viewH.llContainer.setTag(R.id.tag_first, newEditorRmdBean);
        }


        return convertView;
    }

    @Override
    public void onClick(View v) {

        if (v == null) return;
        Object tag = v.getTag(R.id.tag_first);
        if (tag instanceof NewEditorRmdBean) {
            NewEditorRmdBean newEditorRmdBean = (NewEditorRmdBean) tag;
            NewsEntity newsEntity = new NewsEntity();
            newsEntity.setArticleId(newEditorRmdBean.getArticleId());
            newsEntity.setArticleType(newEditorRmdBean.getArticleType());
            TypeConstant.openTypeDetail(newsEntity, (Activity) context, TypeConstant.SOURCETYPE_RECOMMEND);
        }
    }


    class ViewH {
        TextView tvPushTime;
        TextView tvTitile;
        TextView tvDesc;
        CustomImageView civThumb;
        CustomImageView civHead;
        LinearLayout llContainer;


        ViewH(View convertView) {
            tvPushTime = (TextView) convertView.findViewById(R.id.tv_time);
            civThumb = (CustomImageView) convertView.findViewById(R.id.civ_thumb);
            civHead = (CustomImageView) convertView.findViewById(R.id.civ_head);
            tvTitile = (TextView) convertView.findViewById(R.id.tv_title);
            tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
            civHead.setAlpha(1.0f);
            llContainer = (LinearLayout) convertView.findViewById(R.id.ll_container);
            llContainer.setOnClickListener(NewEditorRecommendAdapter.this);
        }

    }
}
